use crate::port::portf::{PF0, PF1, PF2, PF3, PF4, PF5, PF6, PF7};
pub use avr_hal_generic::adc::*;

#[doc(hidden)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[repr(u8)]
pub enum AdcMux {
    Adc0 = 0b000000,
    Adc1 = 0b000001,
    Adc2 = 0b000010,
    Adc3 = 0b000011,
    Adc4 = 0b000100,
    Adc5 = 0b000101,
    Adc6 = 0b000110,
    Adc7 = 0b000111,
}

avr_hal_generic::impl_adc! {
    pub struct Adc {
        type ChannelID = AdcMux;
        peripheral: crate::pac::ADC,
        set_mux: |peripheral, id| {
            let id = id as u8;
            peripheral.admux.modify(|_, w| w.mux().bits(id & 0x1f));
            peripheral.adcsrb.modify(|_, w| w. .mux5().bit(id & 0x20 != 0));
        },
        pins: {
            pf0: (PF0, AdcMux::Adc0, didr0::adc0d),
            pf1: (PF1, AdcMux::Adc1, didr0::adc1d),
            pf2: (PF2, AdcMux::Adc2, didr0::adc2d),
            pf3: (PF3, AdcMux::Adc3, didr0::adc3d),
            pf4: (PF4, AdcMux::Adc4, didr0::adc4d),
            pf5: (PF5, AdcMux::Adc5, didr0::adc5d),
            pf6: (PF6, AdcMux::Adc6, didr2::adc6d),
            pf7: (PF7, AdcMux::Adc7, didr2::adc7d),
        }
    }
}